var base;
if(!base) {
    base = {};
}

base.love = function(self){
    var bid = $(self).attr('bid');
    console.log(bid);
}

base.delete_blog = function(self){
    var bid = $(self).attr('bid');
    var data = {};
    data._id = bid;
    var confirm = window.confirm("确认删除博客?");
    if (confirm === false){
        return ;
    }
    common.notify({
        'flag': true,
        'message': '正在删除博客'
    });
    $.ajax({
        type: 'POST',
        url: '/apis/delete/blog/',
        data: JSON.stringify(data),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8'
    }).done(function(data) {
        if(data.flag == true){
            alert(data.message);
            window.location.href = "/home";
        } else {
            common.notify(data);
        }
    });
}

base.generate_toc = function(){
    console.log('generate toc');
    var re = new RegExp('h\\d+', 'gi');
    var str = 'h1h2h3';
    var tmp = str.match(re);
    console.log(tmp);
    $('#markdown > #-').each(function(){
        console.log($(this).prop('tagName'));
        // console.log($(this).position());
    });

}

base.Toc = {
    // level: 0, 1,2,3,4,5,6
    // level down: 11 12 , 111 112
    element_id: '#markdown',
    init: function(id){
        this.element_id = id;
    },

    /*
     * 得到当前level下所有的元素
     * */
    get_all_elements: function(){
        var selector = this.element_id + ' > #-';
        return $(selector);
    },

    /*
     * 由于get_all_elements中的选择器的原因，理论上永远放回true
     * */
    is_title: function(tag_name){
        console.log(tag_name);
        var re = new RegExp('h\\d', 'gi');
        var flag = tag_name.match(re);
        if (flag != null){
            return true;
        }
        return false;
    },
    find_root: function(level, elements){
        var self = this;
        console.log(elements);
        var root_list = [];     // 保存根节点
        var children_list = [];     // 保存所有节点

        var root_order = 0;
        var last_h_num = 7;
        elements.each(function(){
            var h_num = $(this).prop('tagName').slice(-1);
            if (h_num) {}
        });
    },

    /*
     * 外部调用的唯一接口
     * */
    generate: function(id){
        if (id) {
            this.init(id);
        }
        var elements = this.get_all_elements();
        this.find_root(1, elements)
    }
}

$(document).ready(function(){
    // var toc = Object.create(base.Toc);
    // toc.generate();
});
